// pages/profile/profile.js
import {
  behaviorStore
} from './behavior'
import {
  upload
} from '../../api/upload'
import {
  updateUserInfo
} from '../../../../api/user'
import {
  setStorage
} from '../../../../utils/storage'
import {
  toast
} from "../../../../utils/extendApi"
Page({
  // 页面的初始数据
  behaviors: [behaviorStore],
  data: {
    isShowPopup: false // 控制更新用户昵称的弹框显示与否
  },
  // 更新用户头像
  async chooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail

    const {
      data
    } = await upload(avatarUrl)
    this.setData({
      'userInfo.headimgurl': data
    })
  },
  // 保存更新的用户信息
  async updateUserInfo() {
    await updateUserInfo(this.data.userInfo)
    // 本地化存储
    setStorage('userInfo', this.data.userInfo)
    // 更新store中的用户信息
    this.setUserInfo(this.data.userInfo)
    toast({
      title: "更新成功",
      icon: 'success'
    })
  },
  submitUseiInfo({
    detail: {
      value: {
        nickname
      }
    }
  }) {
    console.log(nickname);
    this.setData({
      'userInfo.nickname': nickname,
      isShowPopup: false
    })
  },
  // 显示修改昵称弹框
  onUpdateNickName() {
    this.setData({
      isShowPopup: true,
      'userInfo.nickname': this.data.userInfo.nickname
    })
  },

  // 弹框取消按钮
  cancelForm() {
    this.setData({
      isShowPopup: false
    })
  }
})